package com.kunyi.web.service.file.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kunyi.platform.tool.exception.BusinessException;
import com.kunyi.web.service.file.core.enums.ExportRequestStatusEnum;
import com.kunyi.web.service.file.core.transform.ExportRequestTransform;
import com.kunyi.web.service.file.entity.domain.ExportRequest;
import com.kunyi.web.service.file.entity.resp.ExportProgressResp;
import com.kunyi.web.service.file.mapper.IExportRequestMapper;
import com.kunyi.web.service.file.service.IExportRequestService;
import org.springframework.stereotype.Service;

/**
 * @ClassName ExportRequestServiceImpl
 * @Description export_request表相关业务实现
 * @Author pengjunjun
 * @Date 2024/03/04
 * @Version 1.0
 **/
@Service
public class ExportRequestServiceImpl extends ServiceImpl<IExportRequestMapper, ExportRequest> implements IExportRequestService {

    @Override
    public Boolean changeStatus(Long id, Integer status) {
        ExportRequest request = new ExportRequest();
        request.setStatus(status);
        request.setId(id);
        return updateById(request);
    }

    @Override
    public Boolean changeSuccessTotal(Long id, int successNum) {
        return baseMapper.updateSuccessTotal(id,successNum) == 1;
    }

    @Override
    public Boolean success(Long id, String fileUrl) {
        ExportRequest exportRequest = new ExportRequest()
                .setStatus(ExportRequestStatusEnum.SUCCESS.getCode())
                .setFileUrl(fileUrl);
        exportRequest.setId(id);
        return updateById(exportRequest);
    }

    @Override
    public ExportProgressResp progress(String code) {
        ExportRequest request = baseMapper.getByCode(code);
        if(request == null){
            throw new BusinessException("没有找到导出的文件");
        }
        return ExportRequestTransform.request2Progress(request);
    }
}
